$(document).ready(function() {
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });

    // 默认的省份市区
    var dataroot = Jurl + "/address.json";
    $.getJSON(dataroot, function(data) {
        var region = data.region;
        var address = data.address;
        var html = "";
        for (var i = 0; i < region.length; i++) {
            html += "<option value='" + region[i].name + "' data='" + region[i].ProID + "'>" + region[i].name + "</option>";
        }
        $("#region").empty();
        $("#region").append(html);
        var regionId = $("#region  option:selected").attr('data');
        var html1 = "";
        for (var i = 0; i < address.length; i++) {
            if (regionId == address[i].ProID) {
                html1 += "<option value='" + address[i].name + "'>" + address[i].name + "</option>";
            }
        }
        $("#address").empty();
        $("#address").append(html1);
    });

    //下拉框改变触发事件
    $("#region").on('change', function() {
        var regionId = $("#region  option:selected").attr('data');
        var dataroot = Jurl + "/address.json";
        $.getJSON(dataroot, function(data) {
            var address = data.address;
            var html1 = "";
            for (var i = 0; i < address.length; i++) {
                if (regionId == address[i].ProID) {
                    html1 += "<option value='" + address[i].name + "'>" + address[i].name + "</option>";
                }
            }
            $("#address").empty();
            $("#address").append(html1);
        });
    });

    //提交表单前的验证的方法
    $("#submit").click(function() {
        if ($(".has-success").length > 0) {
            if ($(".has-error").length == 0) {
                $.ajax({
                    url:"/User/addUser",
                    type: "post",
                    data: $("#commentForm").serialize(),
                    success: function(reg) {
                        if (reg.status == "success") {
                            layer.msg(reg.reson, { icon: 1 });
                            setTimeout(function() {
                                window.location.reload();
                            }, 1500);
                        } else {
                            layer.msg(reg.reson, { icon: 2 });
                        }
                    }
                });
            } else {
                console.log("还有错误");
            }
        }
    });

});
